/*
 * Copyright (C) 2003, Velocityme Partnership
 * AnchorSessionBean.java
 *
 * Created on 24 November 2004, 09:16
 */

package com.velocityme.session;

import com.velocityme.entity.ChangeDeltaTypeBean;
import com.velocityme.entity.PermissionBean;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import com.velocityme.interfaces.*;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.*;
import java.rmi.RemoteException;

/**
 *
 * @author  Robert
 * @ejb.bean
 *           type="Stateless"
 *           cmp-version="2.x"
 *           name="AnchorSession"
 *           jndi-name="ejb/AnchorSession"
 *           view-type="local"
 *           transaction-type="Container"
 * @ejb.transaction type="Required"
 *
 * @ejb.util generate="physical"
 */
public class AnchorSessionBean implements SessionBean {
    
    private SessionContext m_context;
    
    private SearchSessionLocal m_searchSessionLocal;
    
    private PermissionLocal m_permissionAnchorCreate;
    private PermissionLocal m_permissionAnchorEdit;
    private PermissionLocal m_permissionNodeView;

    /**
     * If the user has permission to create a new Anchor in the specified parent
     * node then go ahead and create it and log an appropriate ChangeDelta.
     *
     * @param p_key User's security key
     * @param p_parentNode The node which will contain the new Anchor
     * @param p_anchorValue The value details for the Anchor part of the anchor
     *
     * @ejb.interface-method view-type="local"
     **/
    public AnchorLocal createAnchor(KeySessionLocal p_keyLocal,
                                    NodeLocal p_parentNodeLocal,
                                    com.velocityme.valueobjects.AnchorValue p_anchorValue) 
        throws PermissionDeniedException, InvalidKeyException {
        try {
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, p_parentNodeLocal).contains(m_permissionAnchorCreate)) {
                DBObjectLocal dBObjectLocal = DBObjectUtil.getLocalHome().create();
                TaskValue taskValue = p_anchorValue.getTaskValue();
                taskValue.setType("Anchor");
                taskValue.getNodeValue().setFinalType("Anchor");
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.CREATE, p_keyLocal.getUserLocal(), dBObjectLocal);
                TaskLocal taskLocal = TaskSessionUtil.getLocalHome().create().createTaskHelper(p_keyLocal, p_parentNodeLocal, taskValue, changeDeltaLocal);
                AnchorLocal anchorLocal = AnchorUtil.getLocalHome().create(taskLocal, p_anchorValue);
                return anchorLocal;
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission to edit the anchor then do so and log the
     * changes with an appropriate ChangeDelta.
     *
     * @param p_key User's security key
     * @param p_anchor The Anchor object being edited
     * @param p_anchorValue The value details for the Anchor part of the anchor
     *
     * @ejb.interface-method view-type="local"
     **/
    public void editAnchor(KeySessionLocal p_keyLocal, 
                           AnchorLocal p_anchorLocal,
                           com.velocityme.valueobjects.AnchorValue p_anchorValue,
                           java.util.Collection p_responsibleContactableLocals,
                           java.util.Collection p_interestedContactableLocals)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            TaskLocal taskLocal = p_anchorLocal.getTaskLocal();
            NodeLocal nodeLocal = taskLocal.getNodeLocal();
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, nodeLocal).contains(m_permissionAnchorEdit)) {
                TaskValue taskValue = p_anchorValue.getTaskValue();
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.EDIT, p_keyLocal.getUserLocal(), nodeLocal.getDBObjectLocal());
                TaskSessionUtil.getLocalHome().create().editTaskHelper(p_keyLocal, taskLocal, taskValue, p_responsibleContactableLocals, p_interestedContactableLocals, changeDeltaLocal);
                p_anchorLocal.setAnchorValue(p_anchorValue);
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission to access an anchor then return the local object.
     *
     * @param p_key User's security key
     * @param p_anchorPK The Anchor object to lookup
     *
     * @ejb.interface-method view-type="local"
     **/
    public AnchorLocal getAnchorLocal(KeySessionLocal p_keyLocal, 
                                      AnchorPK p_anchorPK)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            AnchorLocal anchorLocal = AnchorUtil.getLocalHome().findByPrimaryKey(p_anchorPK);
            NodeLocal nodeLocal = anchorLocal.getTaskLocal().getNodeLocal();
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, nodeLocal).contains(m_permissionNodeView)) {
                return anchorLocal;
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(FinderException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * Create the Session Bean.
     * @throws CreateException 
     */
    public void ejbCreate() throws CreateException {
        try {
            m_searchSessionLocal = SearchSessionUtil.getLocalHome().create();
            m_permissionAnchorCreate = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.ANCHOR_CREATE)));
            m_permissionAnchorEdit = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.ANCHOR_EDIT)));
            m_permissionNodeView = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.NODE_VIEW)));
        }
        catch(NamingException e) {
            throw new CreateException(e.getMessage());
        }
        catch(FinderException e) {
            throw new CreateException(e.getMessage());
        }
    }

    public void ejbActivate() throws java.rmi.RemoteException {
    }
    
    public void ejbPassivate() throws java.rmi.RemoteException {
    }
    
    public void ejbRemove() throws java.rmi.RemoteException {
    }
    
    public void setSessionContext(javax.ejb.SessionContext sessionContext) throws java.rmi.RemoteException {
        m_context = sessionContext;
    }
    
}
